package com.oncecloud.devops.client.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.oncecloud.devops.client.dao.EnvMapper;
import com.oncecloud.devops.client.dao.EnvUserMapper;
import com.oncecloud.devops.client.dao.UserMapper;
import com.oncecloud.devops.client.dao.UserRoleMapper;
import com.oncecloud.devops.client.entity.Env;
import com.oncecloud.devops.client.entity.User;
import com.oncecloud.devops.client.service.UserService;
/**
 * 用户服务实现类
 * @author xzg
 *
 */
@Component
public class UserServiceImpl implements UserService{
	private static final Logger LOGGER = Logger.getLogger(UserServiceImpl.class);
	@Autowired
	private UserMapper userMapper;
	@Resource 
	private EnvUserMapper envUserMapper;
	@Autowired
	private EnvMapper envMapper;
	@Resource
	private UserRoleMapper userRoleMapper;
	@Override
	public String getUserDetail(Integer userId) {
		User user = userMapper.getUser(userId);
		return JSONObject.toJSONString(user);
	}

	@Override
	public String getUsers(String userName) {
		User user = new User();
		user.setUserName(userName);
		List<User> users = userMapper.getUsers(user);
		return JSONArray.toJSONString(users);
	}

	@Override
	public int insert(User user) {
		return userMapper.insert(user);
	}

	/**
	 * 删除用户以及相关联的表
	 * @see com.oncecloud.devops.client.service.UserService#delete(com.oncecloud.devops.client.entity.User)
	 */
	@Transactional
	@Override
	public void delete(Integer userId) {
		try{
			 //删除用户
			userMapper.delete(userId);
			//删除用户角色表
			userRoleMapper.deleteByUserId(userId);
			//删除用户环境表
			envUserMapper.deleteEnvByUserId(userId);
			//删除用户项目表
			userMapper.deleteUserProject(userId);
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	@Override
	public int updateUserByUserId(User user) {
		try{
			return userMapper.updateUserByUserId(user);
		}catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		 return 0;
	}

	@Override
	public User getUserByUid(String uid) {
		return userMapper.getUserByUid(uid);
	}

	@Override
	public int updateByUid(User ldapUser) {
		try {
			return userMapper.updateUserByUid(ldapUser);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public User getUserByUserId(Integer userId) {
		return userMapper.getUser(userId);
	}

	@Override
	public List<Env> getEnv(User user) {
		// TODO Auto-generated method stub
 		List<Env> list = new ArrayList<Env>();
		int isAdmin = user.getIsAdmin();
		if(1==isAdmin)
			list = envMapper.getAllEnv();
		else
			try {
				list =  envUserMapper.getEnvByUserId(user.getUserId());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				LOGGER.error(user.getUserId()+"数据库操作异常！");
				e.printStackTrace();
			}
		
		return list;
	}

	/**
	 * 超级管理员查看环境管理员
	 * @see com.oncecloud.devops.client.service.UserService#getEnvUser()
	 */
	@Override
	public List<User> getEnvUser() {
		// TODO Auto-generated method stub
		return userMapper.getEnvRoleUser();
	}

	/**
	 * 查看相应环境下的普通用户
	 * @see com.oncecloud.devops.client.service.UserService#getOrdinaryleUser(java.lang.Integer)
	 */
	@Override
	public List<User> getOrdinaryleUser(Integer envId) {
		// TODO Auto-generated method stub
		
		return userMapper.getOrdinaryleUser(envId);
	}

	/**
	 * 查看相应环境下的项目管理员
	 * @see com.oncecloud.devops.client.service.UserService#getProjectUser(java.lang.Integer)
	 */
	@Override
	public List<User> getProjectUser(Integer envId) {
		
		return userMapper.getProjectRoleUser(envId);
	}

	
	
}
